# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=kopano-core
subpackages="$pkgname-dbg $pkgname-openrc $pkgname-doc $pkgname-bash-completion"
pkgver=10.1.1
pkgrel=12
pkgdesc="Open Source Groupware Solution"
# ppc64le blocked by py3-libmdbx
arch="all !ppc64le"
url="https://www.kopano.com/"
license="AGPL-3.0-or-later"
options="!check" # No test suite
_phpver=7
_php=php$_phpver
makedepends="
	autoconf
	automake
	curl-dev
	db-dev
	docbook-xsl
	e2fsprogs
	gnu-libiconv-dev
	gsoap
	gsoap-dev
	icu-dev
	jsoncpp-dev
	krb5-dev
	libhx-dev
	libical-dev
	libtool
	libvmime-dev
	libxml2-dev
	mariadb-dev
	openldap-dev
	$_php-dev
	py3-setuptools
	python3-dev
	sed
	swig3
	xapian-core-dev
	xmlto
	xorgproto
"
pkgusers="kopano-core kopano-gateway kopano-ical postfix"
pkggroups="kopano http kopano-diraccess"
depends="
	bash-completion
	bison
	boost
	boost-libs
	catdoc
	cyrus-sasl
	gnu-libiconv
	krb5
	libxslt
	mariadb
	openldap
	openrc
	$_php
	$_php-fpm
	poppler
	postfix
	py3-daemon
	py3-dateutil
	py3-dnspython
	py3-flask
	py3-lockfile
	py3-magic
	py3-libmdbx
	py3-minimock
	py3-nose
	py3-openssl
	py3-soappy
	py3-tlslite-ng
	py3-tzlocal
	w3m
	xapian-bindings-$_php
	xapian-bindings-python3
	xorgproto
"

install="$pkgname.pre-install"

source="
	https://github.com/Kopano-dev/kopano-core/archive/kopanocore-$pkgver.zip
	kopano-ical.initd
	kopano-search.initd
	kopano-spooler.initd
	kopano-dagent.initd
	kopano-monitor.initd
	kopano-server.initd
	kopano-gateway.initd
	kopano-spamd.initd
	kopano-server.conf
	0001-python-Use-libmdbx-instead-of-bsddb.patch
	0002-provider.patch
	0003-spamd-Correct-owner-and-group-of-spamd-ham_dir-spam_.patch
	0004-configure.ac-depend-on-libldap-not-libldap_r-doesn-t.patch
	fix-icu-70-1.patch
	fix-build-with-musl-1.2.3.patch
"

builddir="$srcdir/kopano-core-kopanocore-$pkgver/"

prepare() {
	default_prepare
	sed -i 's/0.9.2k1/0.9.2/g' configure.ac

	./bootstrap.sh
}

build() {
	PYTHON=/usr/bin/python3 ./configure \
	--prefix=/usr \
	--localstatedir=/var \
	--sysconfdir=/etc \
	--exec-prefix=/usr \
	--sbindir=/usr/bin \
	--datarootdir=/usr/share \
	--includedir=/usr/include \
	--enable-release \
	--enable-epoll \
	--enable-python \
	--disable-static \
	--with-quotatemplate-prefix=/etc/kopano/quotamails \
	--with-searchscripts-prefix=/etc/kopano/searchscripts \
	--with-php=$_phpver \
	--with-php-config=php-config$_phpver

	make
}

_cfg_set() {
	local config_attribute="$1"
	local config_attribute_prefix=$(echo -n "$config_attribute" | sed "s|\(_\).*|\1|")
	local config_value="$2"
	local config_file="$3"

	# Uncomment And Set Attribute
	if grep -q "$config_attribute" $config_file ;
	then
	# Uncomment (Replaces Optional Comments And Spaces)
	sed -i "s|^#*\s*\($config_attribute.*\)|\1|" $config_file

	# Set ("name = value" => "name = newvalue")
	sed -i "s|^\($config_attribute\)\s*\=.*|\1 = $config_value|" $config_file

	# Add Attribute At Right Position
	elif [ -n "$config_attribute_prefix" ] \
	&& grep -q "$config_attribute_prefix" $config_file ;
	then
	# Find Last Attribut From Prefix-Group
	local config_attribute_last_from_group=$(tac $config_file | grep -m 1 "^#*\s*$config_attribute_prefix" | grep -o "${config_attribute_prefix}[^ =]*")
	sed -i "s|\($config_attribute_last_from_group.*$\)|\1\n$config_attribute = $config_value|" $config_file

	# Add Attribute At The End of File
	else
	echo $config_attribute = $config_value >> $config_file

	fi
}

package() {
	mkdir -p "$pkgdir"
	make install -j1 DESTDIR="$(realpath $pkgdir)"
	mkdir -p $pkgdir/usr/share/bash-completion/completions
	mv $pkgdir/usr/share/bash-completion/completions/kopano-bash-completion.sh $pkgdir/usr/share/bash-completion/completions/kopano

	rm -Rf "$pkgdir/etc/kopano/license" "$pkgdir/usr/lib/sysusers.d/" "$pkgdir/usr/lib/tmpfiles.d/"

	mkdir -p "$pkgdir/usr/share/licenses/$pkgname"
	cp AGPL-3 "$pkgdir/usr/share/licenses/$pkgname"
	mkdir -p "$pkgdir/usr/share/doc/$pkgname"
	cp -R RELNOTES.txt CONTRIBUTING.md  "$pkgdir/usr/share/doc/$pkgname"

	# General
	share_path="$pkgdir/usr/share/doc/kopano/example-config"
	#cfg_path="$pkgdir/etc/kopano"
	cfg_path="$share_path/"
	run_as_user="kopano-core"
	run_as_group="kopano"
	server_socket="/run/kopano/server.sock"
	server_lo_port="tcp://127.0.0.1:236"
	server_socket_prio="/run/kopano/prio.sock"
	search_socket="/run/kopano/search.sock"
	server_tls_min_proto="TLSv1 TLSv1\.1 TLSv1\.2"
	ssl_ciphers="AES256\+EECDH:AES256\+EDH:\!aNULL"
	server_tls_min_proto="tls1\.2"
	gateway_tls_min_proto="tls1\.2"
	ical_tls_min_proto="tls1\.2"
	ssl_privatekey="/etc/ssl/private/kopano.key"
	ssl_certificate="/etc/ssl/private/kopano.crt"

	# admin.cfg
	cfg="$cfg_path/admin.cfg"
	_cfg_set "server_socket" "file://$server_socket" $cfg

	# server.cfg
	cfg="$cfg_path/server.cfg"
	_cfg_set "attachment_compression" "0" $cfg
	_cfg_set "disabled_features" "" $cfg
	_cfg_set "hide_everyone" "yes" $cfg
	_cfg_set "search_enabled" "yes" "$cfg"
	_cfg_set "search_socket" "file://$search_socket" "$cfg"
	_cfg_set "mysql_socket" "/run/mysqld/mysqld.sock" $cfg
	_cfg_set "mysql_user" "kopano" $cfg
	_cfg_set "mysql_password" "kopano" $cfg
	#=> service
	_cfg_set "run_as_user" "$run_as_user" $cfg
	_cfg_set "run_as_group" "$run_as_group" $cfg
	_cfg_set "log_method" "file" $cfg
	_cfg_set "log_file" "/var/log/kopano/server.log" $cfg
	_cfg_set "log_level" "3" $cfg
	#=> server-connection (socket only)
	_cfg_set "server_pipe_name" "$server_socket" $cfg
	_cfg_set "server_pipe_priority" "$server_socket_prio" $cfg
	_cfg_set "server_listen" "" $cfg
	_cfg_set "server_listen_tls" "" $cfg
	_cfg_set "server_ssl_prefer_server_ciphers" "yes" $cfg
	_cfg_set "server_tls_min_proto" "$server_tls_min_proto" $cfg
	_cfg_set "server_ssl_ciphers" "$ssl_ciphers" $cfg
	_cfg_set "server_ssl_key_file" "$ssl_privatekey" $cfg
	_cfg_set "server_ssl_key_pass" "" $cfg
	_cfg_set "server_ssl_ca_file" "$ssl_certificate" $cfg
	_cfg_set "server_ssl_ca_path" "/etc/ssl/certs" $cfg

	# archiver.cfg
	cfg="$cfg_path/archiver.cfg"
	#=> service
	_cfg_set "log_method" "file" $cfg
	_cfg_set "log_file" "/var/log/kopano/archiver.log" $cfg
	_cfg_set "log_level" "3" $cfg
	#=> server-connection
	_cfg_set "server_socket" "file://$server_socket" $cfg

	# backup.cfg
	cfg="$cfg_path/backup.cfg"
	#=> service
	_cfg_set "log_method" "file" $cfg
	_cfg_set "log_file" "/var/log/kopano/backup.log" $cfg
	_cfg_set "log_level" "3" $cfg
	#=> server-connection
	_cfg_set "server_socket" "file://$server_socket" $cfg

	# dagent.cfg
	cfg="$cfg_path/dagent.cfg"
	#=> service
	_cfg_set "run_as_user" "$run_as_user" $cfg
	_cfg_set "run_as_group" "$run_as_group" $cfg
	_cfg_set "log_method" "file" $cfg
	_cfg_set "log_file" "/var/log/kopano/dagent.log" $cfg
	_cfg_set "log_level" "3" $cfg
	#=> server-connection
	_cfg_set "server_bind" "127.0.0.1" $cfg
	_cfg_set "server_socket" "file://$server_socket" $cfg

	# gateway.cfg
	cfg="$cfg_path/gateway.cfg"
	_cfg_set "imap_public_folders" "yes" "$cfg"
	_cfg_set "process_model" "fork" $cfg
	#=> service (avoid requests to be upgraded to admin privileges)
	_cfg_set "run_as_user" "kopano-gateway" $cfg
	_cfg_set "run_as_group" "kopano" $cfg
	_cfg_set "log_method" "file" $cfg
	_cfg_set "log_file" "/var/log/kopano/gateway.log" $cfg
	_cfg_set "log_level" "3" $cfg
	#=> server-connection
	_cfg_set "server_socket" "server_lo_port" $cfg
	_cfg_set "ssl_prefer_server_ciphers" "yes" $cfg
	_cfg_set "tls_min_proto" "$gateway_tls_min_proto" $cfg
	_cfg_set "ssl_ciphers" "$ssl_ciphers" $cfg
	_cfg_set "ssl_private_key_file" "$ssl_privatekey" $cfg
	_cfg_set "ssl_certificate_file" "$ssl_certificate" $cfg

	# ical.cfg
	cfg="$cfg_path/ical.cfg"
	_cfg_set "process_model" "fork" $cfg
	#=> service (avoid requests to be upgraded to kopano-admin)
	_cfg_set "run_as_user" "kopano-ical" $cfg
	_cfg_set "run_as_group" "kopano" $cfg
	_cfg_set "log_method" "file" $cfg
	_cfg_set "log_file" "/var/log/kopano/ical.log" $cfg
	_cfg_set "log_level" "3" $cfg
	#=> server-connection
	_cfg_set "server_socket" "file://$server_socket" $cfg
	_cfg_set "ssl_prefer_server_ciphers" "yes" $cfg
	_cfg_set "tls_min_proto" "$ical_tls_min_proto" $cfg
	_cfg_set "ssl_ciphers" "$ssl_ciphers" $cfg
	_cfg_set "ssl_private_key_file" "$ssl_privatekey" $cfg
	_cfg_set "ssl_certificate_file" "$ssl_certificate" $cfg

	# monitor.cfg
	cfg="$cfg_path/monitor.cfg"
	#=> service
	_cfg_set "run_as_user" "$run_as_user" $cfg
	_cfg_set "run_as_group" "$run_as_group" $cfg
	_cfg_set "log_method" "file" $cfg
	_cfg_set "log_file" "/var/log/kopano/monitor.log" $cfg
	_cfg_set "log_level" "3" $cfg
	#=> server-connection
	_cfg_set "server_socket" "file://$server_socket" $cfg

	# search.cfg
	cfg="$cfg_path/search.cfg"
	_cfg_set "index_attachements" "yes" "$cfg"
	_cfg_set "server_bind_name" "file://$search_socket" "$cfg"
	_cfg_set "ssl_private_key_file" "$ssl_privatekey" $cfg
	_cfg_set "ssl_certificate_file" "$ssl_certificate" $cfg
	#=> service
	_cfg_set "run_as_user" "$run_as_user" $cfg
	_cfg_set "run_as_group" "$run_as_group" $cfg
	_cfg_set "log_method" "syslog" $cfg
	_cfg_set "log_file" "-" $cfg
	_cfg_set "log_level" "3" $cfg
	_cfg_set "pid_file" "/run/kopano/search.pid" $cfg
	#=> server-connection
	_cfg_set "server_socket" "file://$server_socket" $cfg

	# spooler.cfg
	cfg="$cfg_path/spooler.cfg"
	_cfg_set "allow_send_to_everyone" "no" "$cfg"
	#=> service
	_cfg_set "run_as_user" "$run_as_user" $cfg
	_cfg_set "run_as_group" "$run_as_group" $cfg
	_cfg_set "log_method" "syslog" $cfg
	_cfg_set "log_file" "-" $cfg
	_cfg_set "log_level" "3" $cfg
	#=> server-connection
	_cfg_set "server_socket" "file://$server_socket" $cfg

	# spamd.cfg
	cfg="$cfg_path/spamd.cfg"
	_cfg_set "run_as_user" "$run_as_user" $cfg
	_cfg_set "run_as_group" "$run_as_group" $cfg
	#=> server-connection
	_cfg_set "server_socket" "file://$server_socket" $cfg

	install -dm0770 -okopano-core -gkopano "$pkgdir/var/log/kopano"
	install -dm0770 -okopano-core -gkopano-diraccess "$pkgdir/var/lib/kopano"
	install -dm0550 -okopano-core -gkopano "$pkgdir/etc/kopano"

	rm -rf "$pkgdir/usr/lib/systemd"
	# copy init files
	for item in kopano-ical kopano-search kopano-spooler kopano-dagent \
		kopano-monitor kopano-server kopano-gateway kopano-spamd
	do
		install -Dm 755 "$srcdir/$item.initd" "$pkgdir/etc/init.d/$item"
	done
	# copy conf.d file
	install -Dm 644 "$srcdir/kopano-server.conf" "$pkgdir/etc/conf.d/kopano-server"
}

sha512sums="
72983d2147bf37b52230d4cd72bbc3950613b640147f6d8eb045d85556940d5895acc59ce4fbe4b95fc09bf21551b08f51744bb6e82c3df134a830c874e5ade7  kopanocore-10.1.1.zip
0ca7c888b19cf9b33688373b22eb515b103a1d7ab4eb8b9302a4a5b005fa3eaa332f434db1efb372f0d3440567e542ac5ba85e4f8c2dc46597899e3472af506a  kopano-ical.initd
6458f64076bbc398a8cc10a75ed2a9a32851b0a689956b3cb26e7f24364df666f6b173245f90a259a9db0fad8b0436636732c92c2d7fc908e2870fc6cbfad6ef  kopano-search.initd
88b7af9b3e999526d326353b4637a8f64b2684987b22e771d5a3667993e4e715f6722a9ebf17e46fafcdb66520ac8c39dac3f0b16e287053ea05da9946ee6e43  kopano-spooler.initd
a5db7e50f7e43d3d2f282c873cc9d4a4c2035f09cbe75e8b075040f72d63e8c4fcbf3d7c575b2bee0920e4a067c4eb23149144d71375903faeea6a1167a76702  kopano-dagent.initd
3e5d19ff8df78177141bdb79370658e88841affbb5fd9f5f50e0ca5bb9119257c905ca58ac93525b0924a702d3610a5fa40e5f5f3c2e8378d9bcf48b23b31878  kopano-monitor.initd
d962229807e185b82c88465b05f400974273224116216ebd2099cf3ab457e217565ce0839c26ddfdea4eee913b3341b92fd53b02ed30fdd63166a4ac19261a80  kopano-server.initd
b3581c3a93cdd09bae3fbd9070e8263bf61db52348da1c7f1a9ad4657c684af4b83607ea47d036887938ca6d9d609b2ea77d38da00d4173f4051df3eb640be66  kopano-gateway.initd
d19e37a561f192c9d975e0b81341fffccc60f72fabc197eeddbd8423deb93ee83ddadea59c056138c9c6c9ec13e9421cc226719c8e7ca31fa8657eb6ae213405  kopano-spamd.initd
626e40d0a544290aa32210ce15b34ab871edbb670046762004af82dd5db0b3d18cd2f1982af615c393020118133a6b156c3953e5afa85012407efadd4bc49928  kopano-server.conf
fe52eab1c33e4b0469506e8215216131e2d4c45f225dfec6511ab81f0b88180507a4019637691873dc3e2d21ccf9cacc4de1434000e28f480189253ac23a5f44  0001-python-Use-libmdbx-instead-of-bsddb.patch
603409480e56a557dc2031bb6790a72a2fb310937f06acbd637f7d5c4617344bb0e037d3730f8b6b03f25d12ff32f8ef33ab87c2611b33e4865c47529cc103ae  0002-provider.patch
ec72f40ccdb574efcb984ea0aded82668f787eef6d95fd0ca9f953a77d78cb4b3fe04ce233c6dabce72911462d7f11af8aacaa201a78cf125f84970978a5342e  0003-spamd-Correct-owner-and-group-of-spamd-ham_dir-spam_.patch
9ccb1db734905972d96ea3f02fdfab8bfde55b7c43c606dc220a7a84863823a9526c972c5da830ab99e8f3215c84821ee89062e872f7935152e3c92b1d469b82  0004-configure.ac-depend-on-libldap-not-libldap_r-doesn-t.patch
fc793af00cdf28f5c3a5e51a6e51e8c64debc864b5d7b2ae6c5cfb760afed7d34b09d3fe5b04b401457e4ee829b6162358a632ccac23093309e082631195940c  fix-icu-70-1.patch
0510629c00faa2dbbfce980747cf02180aa9480c784f59222815f935917db6fdafb0c07dc0e300aad83e39dc9d6755829a9340b366ffce9b77e8c85b86af30c4  fix-build-with-musl-1.2.3.patch
"
